package zj.com.activities;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.zfdang.multiple_images_selector.SelectorSettings;

import org.xutils.DbManager;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import zj.com.adapter.UploadImgGridAdapter;
import zj.com.db.DBUtil;
import zj.com.model.Gjgx;
import zj.com.model.GjgxDB;
import zj.com.util.LogUtils;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/9.
 *
 * 关键工序编辑
 */
public class GjgxEditActivity extends  BaseActivity implements View.OnClickListener {


    public TextView title_name,right_tv;
    public ImageView left_iv;

    public GridView uploadGridView;
    public List<String> list;
    public UploadImgGridAdapter adapter;
    public static final int REQUEST_CODE = 732;

    public  static ArrayList<String> mResults = new ArrayList<>();


    public EditText question_et,desc_et;
    public TextView xm_tv,gs_tv;
    public RadioGroup rg;

    public List<String> serverList = new ArrayList<>();

    boolean isEdit = false;
    public TextView one_tv,two_tv,three_tv,four_tv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gjgx_edit);
        initView();

    }

    public Gjgx gjgx;
    public int status;
    public void initView(){

        one_tv = (TextView) findViewById(R.id.one_tv);
        two_tv = (TextView) findViewById(R.id.two_tv);
        three_tv = (TextView) findViewById(R.id.three_tv);
        four_tv = (TextView) findViewById(R.id.four_tv);


        isEdit =   getIntent().getBooleanExtra("isEdit",false);
        question_et = (EditText) findViewById(R.id.question_et);
        desc_et = (EditText) findViewById(R.id.desc_et);
        xm_tv = (TextView) findViewById(R.id.xm_tv);
        gs_tv = (TextView) findViewById(R.id.gs_tv);
        rg = (RadioGroup) findViewById(R.id.rg);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup arg0, int arg1) {
                // TODO Auto-generated method stub
                //获取变更后的选中项的ID
                int radioButtonId = arg0.getCheckedRadioButtonId();
                if(radioButtonId == R.id.btn_0){
                    status = 0;
                }else if(radioButtonId == R.id.btn_1){
                    status = 1;
                }else if(radioButtonId == R.id.btn_2){
                    status = 2;
                }
                RadioButton rb = (RadioButton) GjgxEditActivity.this.findViewById(radioButtonId);


                // tv.setText("您的性别是：" + rb.getText());
            }
        });
        title_name = (TextView) findViewById(R.id.title_name);
        title_name.setText("中交三通");
        right_tv = (TextView) findViewById(R.id.right_tv);
        right_tv.setText("保存");
        right_tv.setOnClickListener(this);
        left_iv = (ImageView) findViewById(R.id.left_iv);
        left_iv.setOnClickListener(this);
        left_iv.setVisibility(View.VISIBLE);



        uploadGridView = (GridView) findViewById(R.id.uploadGridView);




        list = new ArrayList<>();
        adapter = new UploadImgGridAdapter(list,this);
        uploadGridView.setAdapter(adapter);

        gjgx = (Gjgx) getIntent().getSerializableExtra("gjgx");
        if(gjgx != null){
            xm_tv.setText(gjgx.getProjectName());
            gs_tv.setText(gjgx.getUnitName());
            one_tv.setText("1. "+gjgx.getName());
            two_tv.setText("2. "+gjgx.getSpecialProject());
            three_tv.setText("3. "+gjgx.getPartProject());
            four_tv.setText("4. "+gjgx.getUnitProject());

            title_name.setText(gjgx.getUnitName());
            serverList =   gjgx.getAttachUrl();
            desc_et.setText(gjgx.getSPRemark());
            status =   gjgx.getStatus();
            if(serverList != null){
                for(String str:serverList){
                    LogUtils.getInstance().error(str);
                }
                list.addAll(serverList);
            }

            list.add("");
            adapter.setDataChange(list);
            if(status == 0){
                rg.check(R.id.btn_0);
            }else if(status == 1){
                rg.check(R.id.btn_1);
            }else if(status == 2){
                rg.check(R.id.btn_2);

            }

        }

    }


    public List<String> localList = new ArrayList<>();
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // get selected images from selector
        if(requestCode == REQUEST_CODE) {
            if(resultCode == RESULT_OK) {
                mResults = data.getStringArrayListExtra(SelectorSettings.SELECTOR_RESULTS);
                assert mResults != null;

                // show results in textview
                StringBuilder sb = new StringBuilder();
                sb.append(String.format("Totally %d images selected:", mResults.size())).append("\n");
                list.clear();
                localList.clear();
                for(String result : mResults) {
                    localList.add(result);
                    list.add(result);
                    sb.append(result).append("\n");

                }
                Log.e("wxf", sb.toString());

                list.add("");
                adapter.setDataChange(list);
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.left_iv:
                finish();
                break;
            case R.id.right_tv:

                save();
                break;
        }
    }

    public void save(){

       String desc =  desc_et.getText().toString();
        if(TextUtils.isEmpty(desc)){

            Toast.makeText(GjgxEditActivity.this, "请输入描述信息", Toast.LENGTH_SHORT).show();
            return;
        }

        if(localList == null || localList.size() == 0){

            Toast.makeText(GjgxEditActivity.this, "请上传本地图片", Toast.LENGTH_SHORT).show();
            return;
        }

        StringBuffer sb = new StringBuffer();
        for(String str:localList){

            sb.append(str);
            sb.append(",");
        }
        try {
            DbManager dbManager = DBUtil.getInstance(this).getDBManager();
            GjgxDB db = new GjgxDB();
            db.setID(UUID.randomUUID().toString());
            db.setGjgxId(gjgx.getID() + "");
            db.setDesc(desc);
            db.setImgs(sb.toString());
            db.setStatus(status);
            db.setTime(gjgx.getStartDate());
            db.setProject(gjgx.getProjectName());
            db.setName(gjgx.getName());
            db.setGs(gjgx.getUnitName());
            db.setMode(1);
            db.setGjgx( gjgx.getUnitProject() + " > " + gjgx.getPartProject() + " > " + gjgx.getSpecialProject()+" > " +gjgx.getName());
            dbManager.saveOrUpdate(db);

            Toast.makeText(GjgxEditActivity.this, " 保存成功", Toast.LENGTH_SHORT).show();
            finish();

        }catch (Exception e){

        }

    }


}
